<?php

class Helper_Csv{
	
	/**
	 * Encode the condition
	 *
	 * @param string|array $sql
	 * @return string
	 */
	function encodeCondition($sql){	
		return base64_encode(serialize($sql));
	}
	
	/**
	 * Decode the sql
	 *
	 * @param string $sql
	 * @return string
	 * 
	 */
	function decodeSql($sql){	
		return unserialize(base64_decode($sql));
	}
	
	/**
	 * export the array to cvs
	 *
	 * @param unknown_type $result
	 */
	function csvOut($result = array()){
		$fname = "ADCS_RESULT_" . date('Ymdhis');
		if(!is_array($result))  $result = array();
		$file_type = "vnd.ms-excel";
		$file_ending = "xls";
		header("Content-Type: application/$file_type");
		header("Content-Disposition: attachment; filename=".$fname.".$file_ending");
		header("Pragma: no-cache");
		header("Expires: 0");	
		$sep = "\t"; 
		array_unshift($result,array_keys($result[0]));
		foreach ($result as $row) { 
			  $schema_insert = ""; 
			  if(is_array($row)){
				  foreach ($row as $v) { 
					  if(!isset($v)) 
						  $schema_insert .= "".$sep; 
					  else 
						  $schema_insert .= $v.$sep; 
				  } 
			  } else {
				  $schema_insert = $row;
			  }
			  $schema_insert = str_replace($sep."$", "", $schema_insert); 
			  $schema_insert .= "\t"; 
			  print(trim($schema_insert)); 
			  print "\n"; 
			  $i++; 
		}
	} 
	
}